package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Coach;
import com.situ.gym.pojo.Course;
import com.situ.gym.pojo.query.CourseQuery;
import com.situ.gym.pojo.vo.CourseVO;
import com.situ.gym.service.ICourseService;
import com.situ.gym.util.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private ICourseService courseService;



    @GetMapping("/selectAll")
    public Result<List<CourseVO>> selectAll() {
        // 直接调用 service 返回带 coachName 的课程列表
        List<CourseVO> list = courseService.selectAllWithCoach();
        return Result.ok(list);
    }


    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    //@RequestMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    //@RequestMapping(value = "/list", method = RequestMethod.GET)
    @GetMapping("/list")
    public Result<IPage<CourseVO>> list(CourseQuery courseQuery) {
        System.out.println("CourseController.list");
        IPage<CourseVO> page = courseService.list(courseQuery);
        return Result.ok(page);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        courseService.removeById(id);
        return Result.ok("删除成功");
    }

    // /deleteAll/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        //courseService.removeBatchByIds(Arrays.asList(ids));
        courseService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    /*@PostMapping("/add")
    public Result add(Course course) {
        System.out.println(course);
        return Result.ok();
    }*/

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Course course) {
        System.out.println(course);
        courseService.save(course);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        return Result.ok(course);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Course course) {
        System.out.println(course);
        //courseService.saveOrUpdate(course);
        courseService.updateById(course);
        return Result.ok("更新成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(status);
        courseService.updateById(course);
        return Result.ok("修改成功");
    }

    //导出
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        courseService.exportExcel(response);
    }

    //导入
    @PostMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        courseService.importExcel(file);
        return Result.ok("导入成功");
    }
}

